package com.kk.init;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
import org.springframework.stereotype.Component;

import java.beans.PropertyDescriptor;

/**
 * 4.实例化感知Bean后处理器
 *
 * <p>
 * .
 *
 * @author little_lunatic
 * @date 2023-08-04
 */
@Slf4j
@Component
public class TestInstantiationAwareBeanPostProcessor implements InstantiationAwareBeanPostProcessor {

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        log.info("4.1 实例化感知Bean后处理器 before initialization [postProcessBeforeInitialization] 初始化前的后处理 " + beanName);
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        log.info("4.2 实例化感知Bean后处理器 after initialization [postProcessAfterInitialization] 初始化后的后处理 " + beanName);
        return bean;
    }

    @Override
    public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
        log.info("4.3 实例化感知Bean后处理器 before instantiation [postProcessBeforeInstantiation] 实例化前的后处理 " + beanName);
        return null;
    }

    @Override
    public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
        log.info("4.4 实例化感知Bean后处理器 after instantiation[postProcessAfterInstantiation] 实例化后的后处理"  + beanName);
        return true;
    }

    @Override
    public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException {
        log.info("4.5 实例化感知Bean后处理器 post process property values [postProcessPropertyValues] 属性值的后处理 " + beanName);
        return pvs;
    }
}
